package com.txl.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.txl.dao.ContactGroupDao;
import com.txl.domain.ContactGroupBean;

public class ContactGroupService {

	private ContactGroupDao dao = new ContactGroupDao();
	
	//查找分组
	public ContactGroupBean findContactGroup(String groupid){

		return dao.find(groupid);
	}
	
	//添加分组
	public void addContactGroup(ContactGroupBean cgb){
		ContactGroupBean parent = dao.find(cgb.getGroupid());//要在哪个父节点下添加子节点
		//创建子节点
		ContactGroupBean child = new ContactGroupBean();
		child.setGroupid(UUID.randomUUID().toString());
		child.setGroupname(cgb.getGroupname());
		child.setLft(parent.getRgt());
		child.setRgt(child.getLft()+1);
		child.setGroupmemo(cgb.getGroupmemo());
		
		dao.updateAfter(parent.getRgt());//更新后续节点
		dao.add(child);//添加节点
		
	}
	
	//修改分组
	public void updateContactGroup(ContactGroupBean cg){
		dao.update(cg);
	}
	
	//删除分组
	public void deleteContactGroup(String groupid){
		dao.delete(groupid);
	}
	
	//获取所有分组
	public List<ContactGroupBean> getAll(){
		return dao.getAll();
	}
	
	//获取2级分组
	public List<ContactGroupBean> get2All(){
		
		List list = new ArrayList();
		for(Object obj : dao.getAll()){
			ContactGroupBean bean = (ContactGroupBean) obj;
			if(bean.getDepth()==2){
				list.add(bean);
			}
		}
		return list;
	}
	
}
